
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZFY.DDD.Application;
using ZFY.UMC.BlazorWebAssembly.Shared.Dto;
using ZFY.UMC.Model;

using ZFY.UMC.Model.Dto.User;
using ZFY.UMC.API.Repository;

namespace ZFY.UMC.API.Service
{
    public class UserService :ApplicationService, IUserService
    {
        private readonly IUserRepository _userRepository;

        public UserService(IUserRepository userRepository)
            
        {
            _userRepository = userRepository;
        }

        public async Task<PageRequestOutput<GetUserListOutput>> GetUserList(GetUserListInput input)
        {
            var query = await _userRepository.GetQueryableAsync();
            var count = await query.CountAsync();
            var list = await query.Include(x=>x.Claims).Skip((input.PageIndex - 1) * input.PageSize).Take(input.PageSize).ToListAsync();

            var dtoList = list.Select(x=>new GetUserListOutput { 
                Email = x.Email,
                Id = x.Id,
                Lock = x.LockoutEnabled,
                Name = x.UserName,
                NickName = x.NickName,
                PhoneNumber = x.PhoneNumber,
                Claims = x.Claims.Select(v=>new UMC.Model.Dto.UserClaim.GetUserClaimListOutput { 
                
                    ClaimType = v.ClaimType,
                    ClaimValue = v.ClaimValue,
                    Id = v.Id
                
                }).ToList()

            }).ToList();

            return new PageRequestOutput<GetUserListOutput>(dtoList, count);
        }
    }
}
